# Copyright 1999-2015 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

DESCRIPTION="Manage the installed gnat compilers"
MAINTAINER="ada@gentoo.org"
SVN_DATE='$Date: 2015/03/31 16:48:57 $'
VERSION=$(svn_date_to_version "${SVN_DATE}" )

# "inheriting" common stuff
# this crude sourcing has to stay the way it is, as common code 
# cannot be part of some eclass (bug #192505)
. /usr/share/gnat/lib/gnat-common.bash


### Phylosophy
# Each gnat installs a "specs" file named ${ARCH}-${PN}-${SLOT} under ${SPECSDIR}
# Each lib creates a dir with its name under ${SPECSDIR} and populates it with
# similar specs files (same name scheme)
# Recognizing gnat specs from lib specs is then eazy - test entry for being a
# dir, then just check what gnat profiles are installed for each lib..
# doset then creates env file that combines settings for gnat and for the
# corresponding profile of each installed lib..


### Helpers

# return *the* name of the active profile, checking that we do not have multiple
# env files.
# There can be only one!
get_current_gnat() {
	local profiles=( $(get_env_list) ) 

	if [ ${profiles[@]} == "${MARKER}*" ]; then exit; fi

	if (( 1 == ${#profiles[@]} )); then
		local active=${profiles[0]#${MARKER}}
	else
		die -q "${ENVDIR} contains multiple gnat profiles, please cleanup!"
	fi

	echo ${active}
#	disabling this check, as we want to use get_current_gnat with empty profile
#	as an indication that last gnat in SLOT was removed
#	if [ -f ${SPECSDIR}/${active} ]; then
#		echo ${active}
#	else
#		die -q "the active env.d profile does not correspond to any installed gnat!"
#	fi
}

# validity check that was removed from get_current_gnat, in case we need it
# somewhere..
# Arguments:
#  $1  - name of the gnat profiel to check
profile_is_valid() {
	if [[ -n $1 ]] ; then
		[[ -f ${SPECSDIR}/$1 ]] || return 1
	fi
}

# check if the passed arg represents the installed gnat and return it or
# not_found
# takes args:
# $1 - list ID to check
get_name_from_list() {
    compiler=$1

    compilers=( $(find_all_compilers) )
    for (( i = 0 ; i < ${#compilers[@]} ; i = i + 1 )) ; do
        if [[ ${compilers[$i]} == ${compiler} ]] ; then
            echo ${compiler}
            return
        fi
    done

    echo "(not-found)"
}


# removes env files (for compiler and libs)
# params:
# $1: the name of profile for which to remove env file
unset_env() {
	rm -f ${ENVDIR}/${MARKER}$1 &> /dev/null
}


### show action ###

describe_show() {
	echo "Show the active gnat compiler/profile and installed libs"
}

do_show() {
	active=$(get_current_gnat)
	if $(profile_is_valid ${active}); then
		write_list_start "Current gnat version:"
		if [[ -n ${active} ]] ; then
			write_kv_list_entry "$active" ""
			libs=( $(find_libs4profile ${active}) )
			write_list_start "Active libs:"
			write_numbered_list "${libs[@]}"
		else
			write_kv_list_entry "(none set)" ""
		fi
	else
		write_error_msg "The active profile in ${ENVDIR} is not valid! Please set a valid profile!"
	fi
}

### list action ###

describe_list() {
	echo "List installed gnat compilers and libs"
}

do_list() {
	compilers=( $(find_all_compilers ) )
	active=$(get_current_gnat)

	write_list_start "Available gnat compilers:"

	if [[ -n ${compilers[@]} ]] ; then
		local i
		for (( i = 0 ; i < ${#compilers[@]} ; i = i + 1 )) ; do
			linkversion=${compilers[${i}]}

			[[ $linkversion == $active ]] && \
				compilers[${i}]="${compilers[${i}]} $(highlight '*' )"
		done
		write_numbered_list "${compilers[@]}"

		# now the libs
		libs=( $(find_all_libs) )
		write_list_start "Installed libs:"
		for (( i = 0 ; i < ${#libs[@]} ; i = i + 1 )) ; do
			[ -f ${SPECSDIR}/${libs[$i]}/${active} ] && \
				libs[${i}]="${libs[${i}]} $(highlight '*' )"
		done
		write_numbered_list "${libs[@]}"
	else
		write_kv_list_entry "(none found)" ""
	fi
}


### set action ###

describe_set() {
	echo "Set active gnat compiler"
}

# The set action. Parameters:
# $1 - name of profile to set, obligatory, passed by eselect
# $2 - optional, name of env file to generate. Used from gnat.eclass.
do_set() {
	if [[ -z ${1} ]] ; then
		# no parameter
		die -q "You didn't tell me which gnat to use"
	fi

	local toset=$(get_name_from_list $1)
	if [[ ${toset} == "(not-found)" ]] ; then
		die -q "I don't recognise the selection"
	fi

	# the action!
	# in this implementation simply create an appropriate env file
	local active=$(get_current_gnat)
	if [[ -z ${2} ]] ; then
		local envfile="${ENVDIR}/${MARKER}${toset}"

		# now we need to remove an old env file, which is guaranteed to 
		# be unique by get_current_gnat above
		if [[ -n ${active} ]] ; then
			unset_env ${active}
		fi
		# just for a good measure remove the one we are going to write
		unset_env ${toset}
	else
		local envfile="$2"
	fi

	generate_envFile ${toset} ${envfile}

	# force update environment
	# should be replaced with "do_action env update" when #172472 gets resolved
	env-update &> /dev/null
}



### update action ###
#
# This action regenerates the /etc/env.d file for the current profile. Basically
# the same as set, only it does not take any parameters. It should be called from ebuilds
# installing Ada libs or in the pkg_postrm in gnatbuild.eclass, to clean up the
# profile if last gnat in SLOT was removed.

describe_update() {
	echo "Update active gnat profile picking up new libs."
}

do_update() {
	local toset=$(get_current_gnat)
	local envfile="${ENVDIR}/${MARKER}${toset}"
	# now check whether to update or unset the profile
	if $(profile_is_valid ${toset}); then
		generate_envFile ${toset} ${envfile}
	else
		unset_env ${toset}
	fi
	do_action env update &> /dev/null
}

### unset action ###

describe_unset() {
	echo "Remove settings for currently active gnat"
}

do_unset() {
	local active=$(get_current_gnat)
	# check whether we have any profile set before removing anything..
	if [[ -n ${active} ]] ; then
		unset_env ${active}
		do_action env update &> /dev/null
	fi
}
